#!/bin/bash
#
# This postflight script echoes the values of the available 
# arguments and environmental variables.
#
#echo "Vars=$1">>"$2/Contents/Resources/config.plist"

echo "Start postflight script"
echo ""
echo "Arguments:"
echo ""
echo "\$1: full path to the installation package"
echo "     $1"
echo "\$2: full path to the installation destination"
echo "     $2"
echo "\$3: mountpoint of the destination volume"
echo "     $3"
echo "\$4: root directory \"/\" for the current System folder"
echo "     $4"
echo ""
echo "Environment variables available to a postflight executable:"
echo "     INSTALLER_TEMP, PACKAGE_PATH, RECEIPT_PATH, SCRIPT_NAME, and TMPDIR"
echo ""
echo "\$INSTALLER_TEMP: scratch area used by Installer for temporary work files"
echo "     $INSTALLER_TEMP"
echo ""
echo "\$PACKAGE_PATH: full path to the installation package; should be same as \$1"
echo "     $PACKAGE_PATH"
echo ""
echo "\$RECEIPT_PATH: full path to directory containing the file being executed"
echo "     $RECEIPT_PATH"
echo ""
echo "\$SCRIPT_NAME: name of the file being executed"
echo "     $SCRIPT_NAME"
echo ""
echo "\$TMPDIR: if set, a path to a location on a writable destination volume"
echo "     $TMPDIR"
echo ""
echo "End postflight script"



AppName="4Media 2D to 3D Video Converter"  
chmod  -R 777 "$2/${AppName}.app"

function dmgpath()
{
    declare -r RELATION_SET=$(hdiutil info | grep -E '^image-path|Apple_HFS')
    declare -r OLD_IFS="$IFS"
    IFS='
'
    local DMG_PATH
    local MOUNT_POINT
    for LINE in ${RELATION_SET}
    do
	if echo "${LINE}" | grep -q '^image-path'
	then
	    DMG_PATH=$(echo "${LINE}" | sed -E -e 's/^image-path.+[[:space:]]+(\/.+\.dmg$)/\1/')
	else
	    MOUNT_POINT=$(echo "${LINE}" | sed -E -e 's/.+[[:space:]]+Apple_HFS[[:space:]]+(\/.+$)/\1/')
	    if echo "${PACKAGE_PATH}" | grep -q "^${MOUNT_POINT}"
	    then
		echo "${DMG_PATH}"
		break
	    fi
	fi
    done

    IFS="${OLD_IFS}"
    
    result=''
	total=`lsof | grep -i -n "$1"`
	total=`echo $total | sed -e s/[0-9]*:/exbice:/g`
	count=`echo $total | awk -F "exbice:" '{print NF-1}'`
	for ((i=1;i<=count;i++));do
	onestr=`echo $total | awk -F "\n" "{print $($NF)}"`
	nextstr=`echo ${total%exbice:*}`
	total=$nextstr
	onestrlower=`echo $onestr | tr '[A-Z]' '[a-z]'`
	lastcomponent=`echo $onestrlower | awk -F "/" '{print $(NF)}'`
	if [ x"${lastcomponent#*$1*.dmg}" != x"$lastcomponent" ];then
		result=`echo $onestr | sed -e "s/^[^\/]*\//\//g"`
	fi
    if [ x$result != x ];then
    break
    fi
	done
	
    echo $result
}

function GetShortCutName
{
    wholefile=`cat "$1"`
    lineno=`echo "$wholefile" | sed  -n -e '/IFPkgShortCut/='`
    nextline=`expr ${lineno} + 1`
    nextstring=`echo "$wholefile" | sed -n "${nextline}"p`
    truestring=${nextstring#*\<string\>}
    truestring=${truestring%\<\/string\>}
    echo "$truestring" 
}

function GetShortCutName2
{
plistname="${1%\.plist}"
shortcutname=`defaults read "$plistname" "IFPkgShortCut"` 
    shortcutname=`echo "$shortcutname" | sed -e "s/\\\\\u\(....\)/\\\\\u\\\1exbice/g"`
    count=`echo $shortcutname | awk -F "exbice" '{print NF-1}'`
    for ((i=1;i<=count;i++));do
    findone=${shortcutname#*"\\u"}
    findone=${findone:0:4}
    DisplayNameSuffix=`echo "\\u$findone" | sed -e 's/.*[a-z] \\\u/\\\u/'| sed 's/\\\u\\(..\\)\\(..\\)/\\\x\\2\\\x\\1/g' | sed 's/.*/\\"\\\xff\\\xfe&\\"/g' | xargs printf | iconv -f utf-16 -t utf-8`
    shortcutname=`echo $shortcutname | sed -e "s/\\\\\\u$findone\\(exbice\)/${DisplayNameSuffix}\\\1/"`
    done
    shortcutname=`echo "$shortcutname" | sed -e "s/exbice//g"`
    echo "$shortcutname"
}

function CreateShortCut
{
    shortcutname=`GetShortCutName "$3"`
    distname="$2/Desktop/$shortcutname"
    sourcename="$1"
    if [ -s "$distname" ];then
        rm -f "$distname"
    fi
    ln -sf "$sourcename" "$distname"
    echo "$shortcutname"    
}

function common_process
{
    cp -rf "$1/Contents/Resources/" "$2/${AppName}.app/Contents/Resources/"

    tmppkgfile=`echo $1 | sed -e "s/[\/|.]/ /g" | awk '{print $(NF-1)}'`

    echo "/tmp/$tmppkgfile.plist @@@"

    tmpfile="/tmp/userprofile"

    if [ -s "/tmp/$tmppkgfile.plist" ];then
	tmpfile="/tmp/$tmppkgfile"
    fi

    if [ -s "$tmpfile.plist" ];then
	HOME=`defaults read "$tmpfile" UserHomeDir`
	BundleName=`defaults read "$2/${AppName}.app/Contents/Info" CFBundleName`
	Identifier=`defaults read "$2/${AppName}.app/Contents/Info" CFBundleIdentifier`
	ProductVersion=`defaults read "$2/${AppName}.app/Contents/Info" Settings.version`
	ProductCode=`defaults read "$2/${AppName}.app/Contents/Info" ProductCode`
	installpacketname=`dmgpath "$ProductCode"`
	last_skin=`defaults read "$2/${AppName}.app/Contents/Info" Settings.last_skin`
	product_version=`defaults read "$2/${AppName}.app/Contents/Info" Settings.version`
	language=`defaults read "$tmpfile" UserLanguage`

	rm -rf "/tmp/tmpprofile.plist"
	PackageInfo=`defaults read "$tmpfile" PackageInfo`
	RET=`defaults write "/tmp/tmpprofile" "$PackageInfo"`
	chmod 777 "/tmp/tmpprofile.plist"
	PacketName=`defaults read "/tmp/tmpprofile" PackageName`
	PacketCode=`defaults read "/tmp/tmpprofile" PackageCode`
	#PacketFileName=`defaults read "/tmp/tmpprofile" PackageFileName`
	PacketFileName=$(dmgpath "$ProductCode")
	PacketVersion=`defaults read "/tmp/tmpprofile" PackageVersion`
	
	
	echo "$PacketName"
	echo "$PacketFileName"
	echo "$PacketCode"
	echo "$PacketVersion"
	PacketName=`echo "$PacketName" | sed "s/ [0-9].*[0-9]//g"`
	
	echo "$installpacketname ~~~~"
	PreferencesFile="$HOME/Library/Preferences/$Identifier"
	RET=`defaults write "$PreferencesFile" "Packets.$PacketName.installpacketname" "$PacketFileName"`
	RET=`defaults write "$PreferencesFile" "Packets.$PacketName.productcode" "$PacketCode"`
	RET=`defaults write "$PreferencesFile" "Packets.$PacketName.productversion" "$PacketVersion"`
	RET=`defaults write "$PreferencesFile" "Settings.OldProductKeyName" "com.${Company_Name}.${Old_Version_Plist}.plist"`
	RET=`defaults write "$PreferencesFile" "Settings.VersionReplace" "true"`
#产品版本号写进imfc
	RET=`defaults write "$PreferencesFile" "Settings.version" "${product_version}"`

	language_ref="English"
	
	if [ x"$language" == xja ];then
	    language_ref="Japanese"
	fi

	if [ x"$language" == xfr ];then
	    language_ref="French"
	fi
	
	selected_language="English"
	
 	if [ -s "$2/${AppName}.app/Contents/Resources/${language}.lproj/Description.plist" ];then
	  ShortCutName=`CreateShortCut "$2/${AppName}.app" "$HOME" "$2/${AppName}.app/Contents/Resources/${language}.lproj/Description.plist"`
	  #ShortCutName=`defaults read "$2/${AppName}.app/Contents/Resources/${language}.lproj/Description" IFPkgShortCut`
	  selected_language=${language}
	  #DisplayNameSuffix=`echo $ShortCutName | sed -e 's/.*[a-z] \\\u/\\\u/'| sed 's/\\\u\\(..\\)\\(..\\)/\\\x\\2\\\x\\1/g' | sed 's/.*/\\"\\\xff\\\xfe&\\"/g' | xargs printf | iconv -f utf-16 -t utf-8`
	  #DisplayNamePrefix=`echo $ShortCutName | sed -e 's/\\\u.*//'`
  else
	  if [ -s "$2/${AppName}.app/Contents/Resources/${language_ref}.lproj/Description.plist" ];then
      ShortCutName=`CreateShortCut "$2/${AppName}.app" "$HOME" "$2/${AppName}.app/Contents/Resources/${language_ref}.lproj/Description.plist"`
      #ShortCutName=`defaults read "$2/${AppName}.app/Contents/Resources/${language_ref}.lproj/Description" IFPkgShortCut`	 
      selected_language=${language_ref}
      #DisplayNameSuffix=`echo $ShortCutName | sed -e 's/.*[a-z] \\\u/\\\u/'| sed 's/\\\u\\(..\\)\\(..\\)/\\\x\\2\\\x\\1/g' | sed 's/.*/\\"\\\xff\\\xfe&\\"/g' | xargs printf | iconv -f utf-16 -t utf-8`
      #DisplayNamePrefix=`echo $ShortCutName | sed -e 's/\\\u.*//'`
    else
		  ShortCutName=`CreateShortCut "$2/${AppName}.app" "$HOME" "$2/${AppName}.app/Contents/Resources/English.lproj/Description.plist"`
	  fi
  fi
  
  if [ x"$ShortCutName" != x ];then
  	RET=`defaults write "$PreferencesFile" "product_name" "$ShortCutName"`
  fi
	
	RET=`defaults write "$2/${AppName}.app/Contents/Info" CFBundleDisplayName "$BundleName"`	

	language_array="English zh_CN ja Japanese French fr"
	
	for language_folder in $language_array
 	do
	    echo "-- $2/${AppName}.app/Contents/Resources/${language_folder}.lproj/InfoPlist.strings"
	    echo "++ $2/${AppName}.app/Contents/Resources/${selected_language}.lproj/InfoPlist.strings"
	    if [ -s "$2/${AppName}.app/Contents/Resources/${language_folder}.lproj/InfoPlist.strings" ];then
		if [ x"${language_folder}" != x"${selected_language}" ];then
		    cp -f "$2/${AppName}.app/Contents/Resources/${selected_language}.lproj/InfoPlist.strings" "$2/${AppName}.app/Contents/Resources/${language_folder}.lproj/InfoPlist.strings"		
		fi
	    fi
	done
	
	chmod 777 "$2/${AppName}.app/Contents/Info.plist"
	
	if [ x"$language" == xen ];then
	    language=en_US
	elif [ x"$language" == xzh-Hant ];then
	    language=zh_TW
	elif [ x"$language" == xzh-Hans ];then
	    language=zh_CN
	elif [ x"$language" == xes ];then
	     language=es
	elif [ x"$language" == xja ];then
	    language=ja
	elif [ x"$language" == xde ];then
	    language=de
	elif [ x"$language" == xfr ];then
	    language=fr
	elif [ x"$language" == xit ];then
	    language=it
	else
	    language=en_US
	fi
	echo "$installpacketname ~~~~"
	PreferencesFile="$HOME/Library/Preferences/$Identifier"
	if [ -s "$PreferencesFile.plist" ];then		
	    last_packetname=`defaults read "$PreferencesFile" "Settings.installpacketname"` 
	    if [ x"$last_packetname" !=  x ];then
		installpacketname="$last_packetname"
	    fi
	fi
	RET=`defaults write "$PreferencesFile" "Settings.version" "$ProductVersion"` 
	echo $RET
	RET=`defaults write "$PreferencesFile" "Settings.installpacketname" -string "$installpacketname"` 
	RET=`defaults write "$PreferencesFile" "Settings.last_skin" "$last_skin"` 
	RET=`defaults write "$PreferencesFile" "Settings.language" "$language"`
	RET=`defaults write "$PreferencesFile" "Installer Language" "$language"`  
	echo "$PreferencesFile Settings.installpacketname $installpacketname=========="
	chmod 777 "$PreferencesFile".plist

	# DocPath="$HOME/Documents/${BundleName}User Data"
	# rm -rf "$DocPath"
	# DocPath="$HOME/Documents/${BundleName} User Data"
	# rm -rf "$DocPath"
	#RET=`"$2/${AppName}.app/Contents/MacOS/SetAutoRun" "$2/${AppName}.app" "$HOME/Library/Preferences/loginwindow.plist"`
	#chmod 755 "$HOME/Library/Preferences/loginwindow.plist"
	#chmod 755 "$PreferencesFile.plist"
    else
	echo "======================"
    fi

    InstallerName="${1##*\/}" 
    OldPackage="/Library/Receipts/$InstallerName/"
    if [ -s "$OldPackage" ]; then
	rm -rf $OldPackage
    fi
    
    rm -rf "/tmp/userprofile.plist"
    rm -rf "/tmp/$tmppkgfile.plist"
}


common_process "$@"

if [ ! -s "$1/Contents/Resources/postflight-app" ]
then
    echo "postflight-app lost"
    exit 1
else
    source "$1/Contents/Resources/postflight-app" "$@"
    application_specific_process "$@"
fi
 
